/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.faytechcc.csc251.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author woodsk9710
 */
public class MTGDatabaseWindow
        extends Window
{
    private JPanel buttonPanel;
    private JButton addButton;
    private JButton deckButton;
    private ResultsPanel resultsPanel;
    private final String query = "SELECT * FROM Card";
    public MTGDatabaseWindow()
    {
        resultsPanel = new ResultsPanel(query);
        this.setResultsPanel(resultsPanel);
        
        buttonPanel = new JPanel();
        addButton = new JButton("Add to Collection");
        addButton.addActionListener(new CollectionInsertListener(resultsPanel, this));
        deckButton = new JButton("Add to Deck");
        buttonPanel.add(addButton);
        buttonPanel.add(deckButton);
        
        this.setButtonPanel(buttonPanel);
        
        this.pack();

        this.setVisible(true);
    }
     public class CollectionInsertListener implements ActionListener {

        private ResultsPanel resultsPanel;
        private MTGDatabaseWindow window;

        public CollectionInsertListener(ResultsPanel rPanel, MTGDatabaseWindow window) {
            this.resultsPanel = rPanel;
            this.window = window;
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            String[] cardInfo = this.resultsPanel.getSelectedCardInfo();
            String sql = "INSERT INTO Collection (Name, SetName, ID, Type,"
                    + " Power, Toughness, Manacost, ConvertedManaCost, Color,"
                    + " Rarity, Ability) VALUES (";
            for(int i = 0; i < cardInfo.length; i++)
            {
                cardInfo[i] = cardInfo[i].replace("'","''");
                sql += "'" + cardInfo[i].trim() + "', ";
            }
            sql = sql.substring(0, sql.length() - 2);
            sql = sql + ")";
            //sql = sql.replace("'","''");
            System.out.println(cardInfo.length);
            System.out.println(sql);
            this.window.executeSQLStatement(sql);
        }
    }
}
